package com.example.spark.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spark.dao.AdviserDao;
import com.example.spark.entity.Adviser;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.AdviserService;
import com.example.spark.service.minio.MinioCloudStorageService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author gky
 * @date 2023/08/26 11:10
 * @description
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AdviserServiceImpl extends ServiceImpl<AdviserDao, Adviser> implements AdviserService {
    @Resource
    MinioCloudStorageService minIoService;
    @Autowired
    private AdviserDao adviserDao;

    @Override
    public CommonResult<IPage<Adviser>> listAdviser(Integer currentPage, Integer pageSize) {
        IPage<Adviser> page = new Page<>(currentPage, pageSize);
        // 如果当前页码值大于总页码值，重查，使用总页码值为当前页码值
        if (currentPage > page.getPages()) {
            page = new Page<>(page.getPages(), pageSize);
        }
        IPage<Adviser> adviserPage = adviserDao.selectPage(page, null);
        return new CommonResult<>(200, "success", adviserPage);
    }

    /**
     * 新增或修改指导老师信息
     * @param adviser 指导老师
     * @return 空
     */
    @Override
    public CommonResult<String> updateAdviser(Adviser adviser) {

        if (adviser.getId() == null) {
            if(adviserDao.insert(adviser)>0){
                return new CommonResult<>(200, "success", "添加成功");
            }else {
                return new CommonResult<>(200, "error", "添加失败");
            }
        } else {
            if(adviserDao.updateById(adviser)>0){
                return new CommonResult<>(200, "success", "修改成功");
            }else {
                return new CommonResult<>(200, "error", "修改失败");
            }
        }

    }

    @Override
    public CommonResult<String> updateImg(Long id, MultipartFile photo) {
        Adviser adviser = adviserDao.selectById(id);
        // 定义图片的存储名称
        String photoName = adviser.getName();
        // 获取文件扩展名
        String photoExtension = FilenameUtils.getExtension(photo.getOriginalFilename());
        // 上传图片并获取图片的存储路径
        String photoUrl;
        try {
            photoUrl = minIoService.uploadPhotoSuffix(photo.getBytes(), photoExtension, photoName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        adviser.setPhoto(photoUrl);
        if(adviserDao.updateById(adviser)>0){
            return new CommonResult<>(200, "success", "修改成功");
        }
        return new CommonResult<>(200, "error", "修改失败");
    }

}
